# encoding=utf8
import sys
import math
reload(sys)
sys.setdefaultencoding('utf8')

class JT_0801:
    result = []
    analysisDescription = ["多媒体数据ID:", "多媒体类型:", "多媒体格式编码:", "事件项编码:", "通道ID:","位置信息汇报（0x0200）消息体:","多媒体数据包:"]
    analysis0801Length = [4,1,1,1,1,28,1]
    WarnInfo = ["非法开门报警", "侧翻预警", "碰撞预警", "车辆非法位移", "车辆非法点火", "车辆被盗(通过车辆防盗器)", "车辆油量异常", "车辆VSS故障",
                "路线偏离报警", "路段行驶时间不足/过长", "进出路线", "进出区域", "超时停车", "当天累计驾驶超时", "保留", "保留", "保留", "疲劳驾驶预警",
                "超速预警", "道路运输证IC卡模块故障", "摄像头故障", "TTS模块故障", "终端LCD或显示器故障", "终端主电源掉电", "终端主电源欠压", "GNSS天线短路",
                "GNSS天线未接或被剪断", "GNSS模块发生故障", "危险预警", "疲劳驾驶", "超速报警", "紧急报警"]
    StatusInfo = [ [ "保留", "保留" ], [ "保留", "保留" ], [ "保留", "保留" ], [ "保留", "保留" ],[ "保留", "保留" ], [ "保留", "保留" ], [ "保留", "保留" ], [ "保留", "保留" ], [ "保留", "保留" ], [ "保留", "保留" ],[ "未使用Galileo卫星进行定位", "使用Galileo卫星进行定位" ], [ "未使用GLONASS卫星进行定位", "使用GLONASS卫星进行定位" ],[ "未使用北斗卫星进行定位", "使用北斗卫星进行定位" ], [ "未使用GPS卫星进行定位", "使用GPS卫星进行定位" ], [ "门5关", "门5开" ],[ "门4关", "门4开" ], [ "门3关", "门3开" ], [ "门2关", "门2开" ], [ "门1关", "门1开" ], [ "车门解锁", "车门加锁" ],[ "车辆电路正常", "车辆电路断开" ], [ "车辆油路正常", "车辆油路断开" ], [ "空车", "半载", "保留", "满载" ], [ "空车", "半载", "保留", "满载" ],[ "保留", "保留" ], [ "保留", "保留" ], [ "经纬度未经保密插件加密", "经纬度已经保密插件加密" ], [ "运营状态", "停运状态" ], [ "东经", "西经" ],[ "北纬", "南纬" ], [ "未定位", "已定位" ], [ "ACC关", "ACC开" ] ]
    WarnStrInfo = ""
    StatusStrInfo = ""
    LatStrInfo = ""
    LongStrInfo = ""
    HeightStrInfo = ""
    SpeedStrInfo = ""
    DireStrInfo = ""
    TimeStrInfo = ""
    s=""
    def LogicAisle(self,protocolStr,header):
        count = 0
        str =""
        for i in range(len(self.analysisDescription)):
            if ( i == 0 or i == 4 ):
                    str = protocolStr[count:count+(self.analysis0801Length[i] * 2)]
                    header.append(self.analysisDescription[i] + str +"\n");
                    count = count + self.analysis0801Length[i] * 2;
            elif i == 1:
                str = protocolStr[count:count + (self.analysis0801Length[i] * 2)]
                if str == "00":
                    header.append(self.analysisDescription[i] + str + " 图像" + "\n")
                elif str == "01":
                    header.append(self.analysisDescription[i] + str + " 音频" + "\n")
                elif str == "02":
                    header.append(self.analysisDescription[i] + str + " 视频" + "\n")
                count = count + self.analysis0801Length[i] * 2
                # 码流类型
            elif i == 2:
                str = protocolStr[count:count + (self.analysis0801Length[i] * 2)]
                if str == "00":
                    header.append(self.analysisDescription[i] + str + " JPEG" + "\n")
                elif str == "01":
                    header.append(self.analysisDescription[i] + str + " TIF" + "\n")
                elif str == "02":
                    header.append(self.analysisDescription[i] + str + " MP3" + "\n")
                elif str == "03":
                    header.append(self.analysisDescription[i] + str + " WAV" + "\n")
                elif str == "04":
                    header.append(self.analysisDescription[i] + str + " WMV" + "\n")
                count = count + self.analysis0801Length[i] * 2
            elif i == 3:
                str = protocolStr[count:count + (self.analysis0801Length[i] * 2)]
                if str == "00":
                    header.append(self.analysisDescription[i] + str + " 平台下指令" + "\n")
                elif str == "01":
                    header.append(self.analysisDescription[i] + str + " 定时动作" + "\n")
                elif str == "02":
                    header.append(self.analysisDescription[i] + str + " 抢劫报警触发" + "\n")
                elif str == "03":
                    header.append(self.analysisDescription[i] + str + " 碰撞侧翻报警触发" + "\n")
                count = count + self.analysis0801Length[i] * 2
            elif i == 5:
                 str = protocolStr[count:count + (self.analysis0801Length[i] * 2)]
                 header.append(self.analysisDescription[i] + str + "\n");
                 an = ['报警标志:', '状态:', '纬度:', '经度:', '高程:', '速度:', '方向:', '时间:']
                 al = [4, 4, 4, 4, 2, 2, 2, 6]
                 c = 0
                 for l in range(8):
                   s = str[c:c + (al[l] * 2)]
                   print (s)
                   if (l == 0):
                    s = self.hexstrtobin(al[l], s)
                    self.AnalysisWarnFlagBits(s)
                    header.append(an[l] + self.WarnStrInfo + "\n")
                   elif (l == 1):
                    s = self.hexstrtobin(al[l], s)
                    self.AnalysisStatusFlagBits(s)
                    header.append(an[l] + self.StatusStrInfo + "\n")
                   elif (l == 2):
                    self.AnalysisLat(s)
                    print(self.LatStrInfo)
                    header.append(an[l] + self.LatStrInfo + "\n")
                   elif (l == 3):
                    self.AnalysisLong(s)
                    header.append(an[l] + self.LongStrInfo + "\n")
                   elif (l == 4):
                    self.AnalysisHeight(s)
                    header.append(an[l] + self.HeightStrInfo + "\n")
                   elif (l == 5):
                    self.AnalysisSpeed(s)
                    header.append(an[l] + self.SpeedStrInfo + "\n")
                   elif (l == 6):
                    self.AnalysisDire(s)
                    header.append(an[l] + self.DireStrInfo + "\n")
                   elif (l == 7):
                     self.AnalysisTime(s)
                     header.append(an[l] + self.TimeStrInfo + "\n")
                   c = c + al[l] * 2
                 count = count + 28 * 2
            elif i == 6 :
                str = protocolStr[count:-4]
                header.append(self.analysisDescription[i] + str + "\n");
        self.result = header
    def hexstrtobin(self, len, str):
                    # 转换为二进制字符串
                    s = bin(int(str, 16))
                    # 位数不足补零
                    n = int(s[2:])
                    n = "%032d" % n
                    print
                    "hexstrtobin.n=%s" % n
                    print
                    "hexstrtobin.n.type=%s" % type(n)
                    # s=str(n)
                    # print "hexstrtobin.s.type=%s"%type(s)
                    return n

    def AnalysisStatusFlagBits(self, source):
        # char[] chars = source.toCharArray();
        # chars = array.array( 'u', source );
        chars = list(source)
        bit8_9 = "";
        print("AnalysisStatusFlagBits.source=%s\n" % source);
        print("AnalysisStatusFlagBits.chars=%s\n" % len(chars));
        for i in reversed(range(len(chars))):
            print("AnalysisStatusFlagBits.i=%s\n" % i);
            print("AnalysisStatusFlagBits.chars=%s\n" % chars[i]);
            if ((len(chars) - i) == 9 or (len(chars) - i) == 10):
                print("AnalysisStatusFlagBits.bit8_9=%s\n" % bit8_9);
                # bit8_9 += String.valueOf(chars[i]);
                bit8_9 += str(chars[i]);
                print("AnalysisStatusFlagBits.bit8_9=%s\n" % bit8_9);
                if ((len(chars) - i) == 10):
                    print("hkkkkkkkkkkkkkkkAnalysisStatusFlagBits.bit8_9=%s\n" % bit8_9);
                    if (bit8_9 == "00"):
                        print("hhhhhhhhhhAnalysisStatusFlagBits.bit8_9=%s\n" % bit8_9);
                        self.StatusInfoAnaly(i, 0);
                    elif (bit8_9 == "01"):
                        self.StatusInfoAnaly(i, 1);
                    elif (bit8_9 == "10"):
                        self.StatusInfoAnaly(i, 2);
                    elif (bit8_9 == "11"):
                        self.StatusInfoAnaly(i, 3);
                continue;

            # self.StatusInfoAnaly(i, Character.getNumericValue(chars[i]));
            # self.StatusInfoAnaly(i, int(chars[i],16));
            self.StatusInfoAnaly(i, int(chars[i]));
    def WarnInfoAnaly(self, num):
        if (self.WarnInfo[num] != "保留"):
                self.WarnStrInfo += self.WarnInfo[num];
                self.WarnStrInfo += "; \n";
    def AnalysisWarnFlagBits(self,source):
        print("AnalysisWarnFlagBits.source=%s"%source);
        print type(source)
        #chars = array.array( 'u', source );
        #char[] chars = source.toCharArray();
        chars =list(source)
        print type(chars)
        print("AnalysisWarnFlagBits.chars=%s"%chars[2]);
        print("AnalysisWarnFlagBits.chars=%d"%len(chars));
        k = True;
        for i in range(len(chars)):
            print("AnalysisWarnFlagBits.chars=%s"%chars[i]);
            if (chars[i] == '1'):
                self.WarnInfoAnaly(i);
                k = False;

    def StatusInfoAnaly(self, i, j):
                    print("StatusInfoAnaly.i=%s" % i);
                    print("StatusInfoAnaly.j=%s" % j);
                    if (self.StatusInfo[i][j] != "保留"):
                        self.StatusStrInfo += self.StatusInfo[i][j];
                        self.StatusStrInfo += "; \n";

    def AnalysisLat(self, source):
        # double Lat;
        try:
            Lat = int(source, 16) * math.pow(10, -6);
            # self.LatStrInfo = String.format("%.6f", Lat);
            self.LatStrInfo = "%.6f" % Lat 
        except:
            pass
    def AnalysisLong(self, source):
                # double Long;
                try:
                    Long = int(source, 16) * math.pow(10, -6);
                    # self.LongStrInfo = String.format("%.6f", Long);
                    self.LongStrInfo = "%.6f" % Long
                except:
                    pass

    def AnalysisHeight(self, source):
                # int height;
                try:
                    height = int(source, 16);
                    self.HeightStrInfo = str(height);
                except:
                    pass

    def AnalysisSpeed(self, source):
                # double speed;
                try:
                    speed = int(source, 16) * math.pow(10, -1);
                    # SpeedStrInfo = String.format("%.2f", speed);
                    self.SpeedStrInfo = "%.1f" % speed
                except:
                    pass

    def AnalysisDire(self, source):
                # int Dire;
                try:
                    Dire = int(source, 16);
                    # DireStrInfo = String.valueOf(Dire);
                    self.DireStrInfo = str(Dire);
                except:
                    pass

    def AnalysisTime(self, source):
                # StringBuilder sb = new StringBuilder (source);
                # sb.insert(0, "20");
                # sb.insert(4, "年");
                # sb.insert(7, "月");
                # sb.insert(10, "日");
                # sb.insert(13, "时");
                # sb.insert(16, "分");
                # sb.insert(19, "秒");
                # TimeStrInfo = sb.toString();
                timelist = list(source)
                timelist.insert(0, "20")
                timelist.insert(3, "年")
                timelist.insert(6, "月")
                timelist.insert(9, "日")
                timelist.insert(12, "时")
                timelist.insert(15, "分")
                timelist.insert(18, "秒")
                self.TimeStrInfo = "".join(timelist)

